package com.jsp.app.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.jsp.app.service.AdminProduct;
import com.jsp.app.service.CategoryService;
import com.tamll.bean.Category;
import com.tamll.bean.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller

public class AdminProductController {

    @Autowired
    private AdminProduct adminProduct;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("ProductPage")
    public String ProductPage1() {
        String product = adminProduct.Product();
        return product;
    }

    @GetMapping("ProductInformation")
    public ModelAndView ProductInformation() {
        Page<Product> productIPage = adminProduct.ProductInformation1();
        List<Product> records = productIPage.getRecords();
        ModelAndView mav = new ModelAndView();
        mav.setViewName("/admin/productManagePage");
        mav.addObject("productList", records);
        return mav;
    }
    @GetMapping("Ca")
    @ResponseBody
    public List<Category> Ca() {
        Page<Category> productIPage = categoryService.Ca1();
        List<Category> records = productIPage.getRecords();
        for (Category product:records){
            System.out.println(product.toString());
        }
        return records;
    }

    @GetMapping("productDetailsPage")
    public String productDetails2(){
     return adminProduct.ProductDetails1();
    }



    @ResponseBody
    @RequestMapping(value = "productDetails", method = RequestMethod.GET)
    public Product goToPage(Integer pid) {
        String url = "http://localhost:9777/productData?pid={pid}";
        Map<String,Integer> map1 = new HashMap<String,Integer>();
        map1.put("pid",pid);
        Product product = restTemplate.getForObject(url, Product.class, map1);
        return product;
    }
}